<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/27
 * Time: 15:38
 */

namespace app\admin\controller;


use app\common\controller\Backend;
use think\Db;
use think\Exception;

/**
 * 红包规则
 * Class Standard
 * @package app\admin\controller
 */
class Standard extends Backend
{
    protected $model = null;

    public function _initialize(){
        parent::_initialize();
        $this->model = model('Standard');
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            unset($value);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            $grade = $this->request->post("grade/a");
            if ($params)
            {
                $this->check($params);
                if(!is_array($grade) || $params['topic_number'] !=count($grade)){
                    $this->error('请勿非法提交！');
                }

                $params['topic_grade'] = implode(',',$grade);
                $this->model->create($params);
                $this->success();
            }
            $this->error('','',$grade);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            $grade = $this->request->post("grade/a");
            if ($params)
            {
                $this->check($params);
                if(!is_array($grade) || $params['topic_number'] !=count($grade)){
                    $this->error('请勿非法提交！');
                }
                $params['topic_grade'] = implode(',',$grade);
                $row->save($params);
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        $topic_grade = explode(',',$row['topic_grade']);
        $this->assignconfig('topic_grade',$topic_grade);
        return $this->view->fetch();
    }

    private function check($data){
        if(!isset($data['money']) || empty($data['money'])){
            $this->error('请输入正确的挑战总金额！');
        }
        if(!isset($data['answer_money']) || empty($data['answer_money'])){
            $this->error('请输入正确的答题费用！');
        }
        if(!isset($data['topic_number']) || empty($data['topic_number'])){
            $this->error('请输入正确的题目数！');
        }
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            Db::startTrans();
            try {
                $result = $this->model->destroy($ids);
                if($result){
                    Db::commit();
                    $this->success('删除成功!');
                }else {
                    $this->error('删除失败!');
                }
            }catch (Exception $e){
                Db::rollback();
                $this->error('删除失败!');
            }

        }
        $this->error();
    }
}